package de.ludo.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Encrypt {

	private MessageDigest md5;
	private byte[] digest;

	public Encrypt(String algorithm) {
		try {
			md5 = MessageDigest.getInstance(algorithm);
		} catch (NoSuchAlgorithmException nsae) {
			nsae.printStackTrace();
		}
	}

	public String toHexString(byte b) {
		int value = (b & 0x7F) + (b < 0 ? 128 : 0);

		String ret = (value < 16 ? "0" : "");
		ret += Integer.toHexString(value).toUpperCase();

		return ret;
	}

	public String do_checksum(String data) {
		StringBuffer strbuf = new StringBuffer();

		md5.update(data.getBytes(), 0, data.length());
		digest = md5.digest();

		for (int i = 0; i < digest.length; i++) {
			strbuf.append(toHexString(digest[i]));
		}

		return strbuf.toString();
	}
}